package net.hhk.controller;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import net.hhk.controller.lazyload.ImageLoader;
import net.hhk.data.MySharePreferences;
import net.hhk.data.User;
import net.hhk.twitter.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class UserAdapter extends BaseAdapter {
	private List<User> socialFeed;
	private Context context;
	private ImageLoader imageLoader;
	private Activity activity;
	private static LayoutInflater inflater = null;
	private int myid = 0;

	public UserAdapter(Activity context, List<User> socialFeed,int myid) {
		super();
		this.socialFeed = socialFeed;
		this.context = context;
		imageLoader = new ImageLoader(context);
		this.activity = context;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(context);
		this.myid =myid;

	}

	public View getView(final int position, final View convertView,
			final ViewGroup parent) {
		View row = convertView;
		if (row == null) {

			row = inflater.inflate(R.layout.item_follow_layout, null);
		}

		User user = (User) getItem(position);

		final TextView profileName = (TextView) row
				.findViewById(R.id.item_follow_fullname);
		profileName.setText(user.getFullname());
		final TextView username = (TextView) row
				.findViewById(R.id.item_follow_username);
		username.setText(" @" + user.getUsername());
		System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
		final ImageView image = (ImageView) row
				.findViewById(R.id.item_follow_image);

		imageLoader.DisplayImage(user.getAvatar(), image);

		System.out
				.println("fffffffffffffffuuuuuuuuuuuuuuucccccccccckkkkkkkkkkkkk");
		final ImageButton follow = (ImageButton) row
				.findViewById(R.id.item_follow_btn);
		follow.setFocusable(false);
		System.out.println("id "+ user.getUserid()+"    "+myid);
		if (user.getUserid() == myid) {
			follow.setVisibility(View.GONE);
		} else {
			if (user.isLastStatus()) {

				follow.setImageResource(R.drawable.ic_follow_checked);
			} else
				follow.setImageResource(R.drawable.ic_follow_default);
			// follow.setOnClickListener(mOnTitleClickListener);
			follow.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					// TODO Auto-generated method stub
					User user = (User) getItem(position);

					if (user.isLastStatus()) {
						user.setLastStatus(false);
						follow.setImageResource(R.drawable.ic_follow_default);

						System.out.println("vvvvvvvvvvvvvvvvvv");
					} else {
						user.setLastStatus(true);
						follow.setImageResource(R.drawable.ic_follow_checked);

						System.out.println("bbbbbbbbbbbbbbbbbbbbb");
					}

					System.out.println("aaaaaaaaaaa" + user.getUserid());

				}
			});
		}

		return row;
	}

	public void appendItems(ArrayList items) {
		socialFeed.addAll(items);
		notifyDataSetChanged();
	}

	public int getCount() {
		return socialFeed.size();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItem(int)
	 */

	public Object getItem(final int index) {

		return socialFeed.get(index);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItemId(int)
	 */

	public long getItemId(final int index) {
		return index;
	}

	public void clearCache() {
		imageLoader.clearCache();
	}

	public int getMyid() {
		return myid;
	}

	public void setMyid(int myid) {
		this.myid = myid;
	}

}
